
// 去除时间的秒
exports.hideSeconds = function (val) {
  if (!val) {
    return ''
  }
  var reg = /(.*):(.*):(.*)/
  val = val.replace(reg, function (match, $1, $2, $3) {
    return $1 + ':' + $2
  })
  return val
}

// 去除时间
exports.hideTime = function (val) {
  if (!val) {
    return ''
  }
  var reg = /(.*)\s(.*):(.*):(.*)/
  val = val.replace(reg, function (match, $1, $2, $3, $4) {
    return $1
  })
  return val
}

// 'yyyy-MM-dd hh:mm:ss
function formatDate (dateObj, format) {
  var date = {
    'M+': dateObj.getMonth() + 1,
    'd+': dateObj.getDate(),
    'h+': dateObj.getHours(),
    'm+': dateObj.getMinutes(),
    's+': dateObj.getSeconds(),
    'q+': Math.floor((dateObj.getMonth() + 3) / 3),
    'S+': dateObj.getMilliseconds()
  }
  if (/(y+)/i.test(format)) {
    format = format.replace(RegExp.$1, (dateObj.getFullYear() + '').substr(4 - RegExp.$1.length))
  }
  for (var k in date) {
    if (new RegExp('(' + k + ')').test(format)) {
      format = format.replace(RegExp.$1, RegExp.$1.length === 1
        ? date[k] : ('00' + date[k]).substr(('' + date[k]).length))
    }
  }
  return format
}

// 时间戳转为时间 yyyy-MM-dd hh:mm:ss
exports.timestampToDate = function (value, format, lang) {
  if (!value || value == '') { // eslint-disable-line
    return ''
  }
  format = format || 'yyyy-MM-dd hh:mm:ss'
  // format = format || 'MM-dd-hh-mm'
  var date = new Date()
  if (lang && lang === 'en') {
    value -= 28800
  }
  date.setTime(value * 1000)
  var dateStr = formatDate(date, format)
  return dateStr
}

// 保留两位小数
exports.toFixed = function (value, length) {
  if (!value) {
    return '0.00'
  }
  if (typeof value === 'string') {
    value = parseFloat(value)
  }
  return value.toFixed(length)
}

// 200.00 EOS => 200.00 、 EOS
exports.sliceNumUnite = function (value, type) {
  if (!value) {
    return ''
  }
  var tempArry = value.split(' ')
  if (type === 'num') {
    return tempArry[0]
  } else {
    return tempArry[1]
  }
}

exports.getGameId = function (data, type) {
  if (!data) return ''
  var curGame = data.find(item => item.game === type)
  if (curGame) {
    var gameid = curGame.gameid
    return gameid % (type * 100000000)
  }
  return ''
}

exports.formatGaimeId = function (val) {
  if (!val) {
    return ''
  } else {
    val = val * 1
    return val % 100000000
  }
}

exports.filterNoticeTitle = function (data, lang) {
  if (!data || !data.post_title) {
    return ''
  }
  if (lang === 'en') {
    return data.post_source || ''
  } else if (lang === 'zh') {
    return data.post_title || ''
  } else {
    return (data.post_title + '').s2t()
  }
}

exports.amountToEos = function (data) {
  return (data / 10000) + ' EOS'
}
